package com.hh.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;

/**
 * 系统用户
 *
 * @TableName sys_user
 */
@ApiModel(value = "com-hh-entity-SysUser")
@TableName("sys_user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Accessors(chain = true)
@Builder
public class SysUser implements Serializable, UserDetails {

    /**
     * ID
     */
    @NotNull(message = "[]不能为空")
    @TableId(value = "user_id", type = IdType.AUTO)
    @ApiModelProperty("ID")
    private Long userId;
    /**
     * 用户名
     */
    @NotBlank(message = "[用户名]不能为空")
    @Size(max = 50, message = "编码长度不能超过50")
    @ApiModelProperty("用户名")
    @Length(max = 50, message = "编码长度不能超过50")
    private String username;
    /**
     * 密码
     */
    @Size(max = 100, message = "编码长度不能超过100")
    @ApiModelProperty("密码")
    @Length(max = 100, message = "编码长度不能超过100")
    private String password;
    /**
     * 邮箱
     */
    @Size(max = 100, message = "编码长度不能超过100")
    @ApiModelProperty("邮箱")
    @Length(max = 100, message = "编码长度不能超过100")
    private String email;
    /**
     * 手机号
     */
    @Size(max = 100, message = "编码长度不能超过100")
    @ApiModelProperty("手机号")
    @Length(max = 100, message = "编码长度不能超过100")
    private String mobile;
    /**
     * 状态  0：禁用   1：正常
     */
    @ApiModelProperty("状态  0：禁用   1：正常")
    private Integer status;
    /**
     * 创建者ID
     */
    @ApiModelProperty("创建者ID")
    private Long createUserId;
    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    private Date createTime;
    /**
     * 用户所在的商城Id
     */
    @ApiModelProperty("用户所在的商城Id")
    private Long shopId;

    /**
     * 权限集合
     */
    @TableField(exist = false)
    private List<String> auths;

    /**
     * 封装权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        // 变成oauth认识的对象
        if (CollectionUtils.isEmpty(auths)) {
            return Collections.emptyList();
        }
        // 循环封装
        auths.forEach(auth -> {
            if (!StringUtils.isEmpty(auth)) {
                // 分割
                String[] split = auth.split(",");
                for (String s : split) {
                    // 循环封装
                    simpleGrantedAuthorities.add(new SimpleGrantedAuthority(s));
                }
            }
        });
        return simpleGrantedAuthorities;
    }

    // SecurityContextHolder 拿到当前用户信息

    /**
     * 因为一个系统需要用户id的
     * 用oauth生成的jwt格式很严格 不可以轻易改变
     * @return
     */
    public String getUsername() {
        return String.valueOf(this.userId);
    }

    public String getPassword() {
        System.err.println("密码是：" + password);
        return this.password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}
